#import "ICE_View_Interface.h"
#import "Globalize.h"

/*
@implementation ICE_View

- (void)create_SubViews
{
}

- (void)destroy_SubViews
{
}

@end
*/

static BOOL g_got_device_type = NO;
static DEVICE_TYPE g_device_type = NO;

DEVICE_TYPE get_device_type()
{
    if (!g_got_device_type)
    {
        CGFloat width, height;
        UIScreen* screen = [UIScreen mainScreen];
        CGRect full_screen = [screen applicationFrame];		
        BOOL is_scale = [screen respondsToSelector:@selector(scale)];
        if (is_scale) {
            CGFloat scale = screen.scale;
            width = full_screen.size.width * scale;
            height = full_screen.size.height * scale;
        }else{
            width = full_screen.size.width;
            height = full_screen.size.height;
        }
        width = width > height ? width : height;
        if (width > 480.f)
        {
            if (width > 960.f)
            {
                g_device_type = DVC_TYPE_IPAD;
            }
            else
            {
                g_device_type = DVC_TYPE_IPHONE4;
            }
        }
        else
        {
            g_device_type = DVC_TYPE_IPHONE3;
        }
        g_got_device_type = YES;
    }
    return g_device_type;
}

#define CALL_SELF_DEVICE_SCALE() switch (get_device_type())\
{\
case DVC_TYPE_IPHONE3:\
   [self scale_to_fit_iPhone3];\
    break;\
case DVC_TYPE_IPHONE4:\
    [self scale_to_fit_iPhone4];\
    break;\
case DVC_TYPE_IPAD:\
    [self scale_to_fit_iPad];\
    break;\
default:\
    break;\
}

@implementation ICE_View
@synthesize globalize_id;
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end
 
@implementation ICE_Image_View
@synthesize globalize_id;
- (void)setGlobalize_id:(int32_t)gid
{
    [[Globalize current_global]localize_image_by_current_language:self of_id:gid];
}
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end

@implementation ICE_Button_View
@synthesize globalize_id;
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView* top_view = self;
    UIResponder* pctl = nil;
    while (top_view != nil)
    {
        //go to top view
        pctl = [top_view nextResponder];
        if (pctl && [pctl isKindOfClass:[UIView class]])
        {
            top_view = (UIView*)pctl;
            continue;
        }
        //get controller
        if (pctl && [pctl isKindOfClass:[UIViewController class]])
        {
            [pctl touchesBegan:touches withEvent:event];
            break;
        }
        break;
    }
    //just told superclass
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView* top_view = self;
    UIResponder* pctl = nil;
    while (top_view != nil)
    {
        //go to top view
        pctl = [top_view nextResponder];
        if (pctl && [pctl isKindOfClass:[UIView class]])
        {
            top_view = (UIView*)pctl;
            continue;
        }
        //get controller
        if (pctl && [pctl isKindOfClass:[UIViewController class]])
        {
            [pctl touchesMoved:touches withEvent:event];
            break;
        }
        break;
    }
    //just told superclass
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView* top_view = self;
    UIResponder* pctl = nil;
    while (top_view != nil)
    {
        //go to top view
        pctl = [top_view nextResponder];
        if (pctl && [pctl isKindOfClass:[UIView class]])
        {
            top_view = (UIView*)pctl;
            continue;
        }
        //get controller
        if (pctl && [pctl isKindOfClass:[UIViewController class]])
        {
            [pctl touchesCancelled:touches withEvent:event];
            break;
        }
        break;
    }
    //just told superclass
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView* top_view = self;
    UIResponder* pctl = nil;
    while (top_view != nil)
    {
        //go to top view
        pctl = [top_view nextResponder];
        if (pctl && [pctl isKindOfClass:[UIView class]])
        {
            top_view = (UIView*)pctl;
            continue;
        }
        //get controller
        if (pctl && [pctl isKindOfClass:[UIViewController class]])
        {
            [pctl touchesEnded:touches withEvent:event];
            break;
        }
        break;
    }
    //just told superclass
    [super touchesEnded:touches withEvent:event];
}
@end

@implementation ICE_Label_View
@synthesize globalize_id;
- (void)setGlobalize_id:(int32_t)gid
{
    [[Globalize current_global]localize_label_by_current_language:self of_id:gid];
}
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end

@implementation ICE_Hidable_Label_View

@synthesize hidden_outside;

#define MAX_HIDABLE_LABEL (128)
static void* g_lbls[MAX_HIDABLE_LABEL] = {0};
static int g_lbls_cnt = {0};
static BOOL g_lbls_hide_all = NO;

- (void)super_hidden:(BOOL)hidden
{
    super.hidden = hidden;
}

+ (void)Enable_Hide_All:(BOOL)enabled
{
    ICE_Hidable_Label_View* item = NULL;
    if (enabled)
    {
        for (int i = 0; i < MAX_HIDABLE_LABEL; ++i)
        {
            if (g_lbls[i])
            {
                item = g_lbls[i];
                [item super_hidden:TRUE];
            }
        }
    }
    else
    {
        for (int i = 0; i < MAX_HIDABLE_LABEL; ++i)
        {
            if (g_lbls[i])
            {
                item = g_lbls[i];
                [item super_hidden:item.hidden_outside];
            }
        }
    }
    g_lbls_hide_all = enabled;
}

- (void)create_SubViews:(UIViewController*)controller
{
    for (int i = 0; i < MAX_HIDABLE_LABEL; ++i)
    {
        if (!g_lbls[i])
        {
            g_lbls[i] = (void*)self;
            break;
        }
    }
    if (g_lbls_hide_all)
    {
        super.hidden = TRUE;
    }
    g_lbls_cnt++;
    CALL_SELF_DEVICE_SCALE();
}

- (void)destroy_SubViews
{
    for (int i = 0; i < MAX_HIDABLE_LABEL; ++i)
    {
        if (g_lbls[i] == (void*)self)
        {
            g_lbls[i] = 0;
        }
    }
    g_lbls_cnt--;
}

- (void)setHidden:(BOOL)hidden
{
    hidden_outside = hidden;
    if (!g_lbls_hide_all)
    {
        super.hidden = hidden;
    }
}

@end

@implementation ICE_Table_View
@synthesize globalize_id;
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end

@implementation ICE_Switch_View
@synthesize globalize_id;
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end

@implementation ICE_View_Controller

@end


@implementation ICE_Table_View_Manager

@end

@implementation ICE_Table_View_Cell
@synthesize globalize_id;
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end

@implementation ICE_Text_Feild
@synthesize globalize_id;
- (void)create_SubViews:(UIViewController*)controller
{
    CALL_SELF_DEVICE_SCALE();
}
- (void)destroy_SubViews
{}
- (void)scale_to_fit_iPhone4
{}
- (void)scale_to_fit_iPhone3
{}
- (void)scale_to_fit_iPad
{}
@end


@implementation ICE_String
@synthesize globalize_id;
@synthesize string;
@synthesize length;
- (void)setGlobalize_id:(int32_t)gid
{
    [[Globalize current_global]localize_string_by_current_language:self of_id:gid];
}

- (id)init
{
    string = malloc(MAX_ICE_STRING_LEN);
    return self;
}

- (void)replaceAllString:(NSString*)str
{
    memset(string, 0, MAX_ICE_STRING_LEN);
    if (str)
    {
        [str getCString:string maxLength:MAX_ICE_STRING_LEN encoding:NSUTF8StringEncoding];
    }
}

- (void)setString:(char *)str
{
    strncpy(string, str, MAX_ICE_STRING_LEN);
    length = strlen(string);
}

- (void)setLength:(int)len
{
    length = len;
}

- (void)dealloc
{
    free(string);
    [self dealloc];
}

@end

@implementation ICE_Image
@synthesize globalize_id;
@end